#include "ShellBullet.h"

ShellBullet::ShellBullet()
{
    static SDL_Texture *tex_shell = ResourcesManager::Instance().texturePool[ResID::Tex_BulletShell];
    static SDL_Texture *tex_explode = ResourcesManager::Instance().texturePool[ResID::Tex_EffectExplode];

    static const std::vector<int> idx_list = {0, 1};
    static const std::vector<int> idx_explode_list = {0, 1, 2, 3, 4};

    animation.SetLoop(true);
    animation.SetInterval(0.1);
    animation.SetFrameData(tex_shell, 2, 1, idx_list);

    animationExplode.SetLoop(false);
    animationExplode.SetInterval(0.1);
    animationExplode.SetFrameData(tex_explode, 5, 1, idx_explode_list);
    animationExplode.SetOnFinished([&]() { MakeInvalid(); });

    damageRange = 96;
    size.x = 48, size.y = 48;
}

void ShellBullet::OnUpdate(double delta)
{
    if (CanCollide()) {
        Bullet::OnUpdate(delta);
        return;
    }
    animationExplode.OnUpdate(delta);
}

void ShellBullet::OnRender(SDL_Renderer *renderer)
{
    if (CanCollide()) {
        Bullet::OnRender(renderer);
        return;
    }
    static SDL_Point point;
    point.x = (int) (position.x - 96 / 2);
    point.y = (int) (position.y - 96 / 2);
    animationExplode.OnRender(renderer, point);
}

void ShellBullet::OnCollide(Enemy *enemy)
{
    static const ResourcesManager::SoundPool &sound_pool = ResourcesManager::Instance().soundPool;
    Mix_PlayChannel(-1, sound_pool.find(ResID::Sound_ShellHit)->second, 0);
    DisableCollide();
}